HAL HRTIM initialization and configuration functions migration

The goal of this page is to show how to migrate the initialization sequence using STM32CubeMX2:

  • Open STM32CubeMX2 HRTIM configuration panel.

  • Configure the SW layer HAL.

  • Configure the HRTIM main features.

  • Configure the HRTIM channels.

  • Configure the HRTIM advanced features.

  • Configure the HRTIM DMA(s) if the application requires to use the HRTIM in DMA mode.

  • Configure the NVIC if the application requires the HRTIM to be used in IT mode.

  • Generate the code.

HAL HRTIM configuration panel

Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.

Project configuration panel

HAL HRTIM main features configuration

Configure the HRTIM main features.

HRTIM main feature configuration

HAL HRTIM channels configuration

Enable and configure the chosen HRTIM channel.

HRTIM channels configuration

HAL HRTIM advanced features configuration

Configure the advanced HRTIM features.

HRTIM advanced feature configuration

HAL HRTIM security and privilege features configuration

Configure the HRTIM security and privilege features if needed.

HRTIM security and privilege feature configuration

GPIO configuration

Configure GPIO pins.

HRTIM GPIO configuration

Note

The HRTIM GPIO configuration is applicable only if a channel is activated.

DMA configuration

Configure the DMA for data reception as needed.

HRTIM DMA configuration

Note

The HRTIM DMA configuration is applicable only if a timer is activated.

NVIC configuration

Configure the NVIC in case of an interrupt.

HRTIM NVIC configuration

Generated HRTIM initialization and de-initialization sequences

The HRTIM initialization and de-initialization sequences are generated in the mx_hrtim1_init function in mx_hrtim.c file under $YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.

Related concepts:

STM32CubeMX2 Generated with user values

Topic

HAL1 code snippet

HAL2 code snippet

Handle declaration

HRTIM_HandleTypeDef hhrtim;
DMA_HandleTypeDef hdma_hrtim1_a;
static hal_hrtim_handle_t hHRTIM1;
static hal_dma_handle_t hGPDMA1_CH0;

Initialization and configuration sequence

static void MX_HRTIM_Init(void)
 {

   HRTIM_TimeBaseCfgTypeDef pTimeBaseCfg = {0};
   HRTIM_SimpleOCChannelCfgTypeDef pSimpleOCChannelCfg = {0};

   hhrtim.Instance = HRTIM1;
   hhrtim.Init.HRTIMInterruptResquests = HRTIM_IT_NONE;
   hhrtim.Init.SyncOptions = HRTIM_SYNCOPTION_NONE;
   if (HAL_HRTIM_Init(&hhrtim) != HAL_OK)
   {
     Error_Handler();
   }
   pTimeBaseCfg.Period = 0xFFFD;
   pTimeBaseCfg.RepetitionCounter = 0x00;
   pTimeBaseCfg.PrescalerRatio = HRTIM_PRESCALERRATIO_DIV1;
   pTimeBaseCfg.Mode = HRTIM_MODE_CONTINUOUS;
   if (HAL_HRTIM_TimeBaseConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_A, &pTimeBaseCfg) != HAL_OK)
   {
     Error_Handler();
   }
   pSimpleOCChannelCfg.Mode = HRTIM_BASICOCMODE_TOGGLE;
   pSimpleOCChannelCfg.Pulse = 0xFFFD;
   pSimpleOCChannelCfg.Polarity = HRTIM_OUTPUTPOLARITY_HIGH;
   pSimpleOCChannelCfg.IdleLevel = HRTIM_OUTPUTIDLELEVEL_INACTIVE;
   if (HAL_HRTIM_SimpleOCChannelConfig(&hhrtim, HRTIM_TIMERINDEX_TIMER_A, HRTIM_OUTPUT_TA1, &pSimpleOCChannelCfg) != HAL_OK)
   {
     Error_Handler();
   }

   HAL_HRTIM_MspPostInit(&hhrtim);

 }

  void HAL_HRTIM_MspInit(HRTIM_HandleTypeDef* hhrtim)
  {
    RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
    if(hhrtim->Instance==HRTIM1)
    {

    /** Initializes the peripherals clock */
      PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_HRTIM1;
      PeriphClkInitStruct.Hrtim1ClockSelection = RCC_HRTIM1CLK_TIMCLK;
      if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
      {
        Error_Handler();
      }

      /* Peripheral clock enable */
      __HAL_RCC_HRTIM1_CLK_ENABLE();

      /* HRTIM1 DMA Init */
      /* HRTIM1_A Init */
      hdma_hrtim1_a.Instance = DMA1_Stream0;
      hdma_hrtim1_a.Init.Request = DMA_REQUEST_HRTIM_TIMER_A;
      hdma_hrtim1_a.Init.Direction = DMA_PERIPH_TO_MEMORY;
      hdma_hrtim1_a.Init.PeriphInc = DMA_PINC_DISABLE;
      hdma_hrtim1_a.Init.MemInc = DMA_MINC_ENABLE;
      hdma_hrtim1_a.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
      hdma_hrtim1_a.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
      hdma_hrtim1_a.Init.Mode = DMA_NORMAL;
      hdma_hrtim1_a.Init.Priority = DMA_PRIORITY_LOW;
      hdma_hrtim1_a.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
      if (HAL_DMA_Init(&hdma_hrtim1_a) != HAL_OK)
      {
        Error_Handler();
      }

      __HAL_LINKDMA(hhrtim,hdmaTimerA,hdma_hrtim1_a);

      /* HRTIM1 interrupt Init */
      HAL_NVIC_SetPriority(HRTIM1_TIMA_IRQn, 0, 0);
      HAL_NVIC_EnableIRQ(HRTIM1_TIMA_IRQn);
    }

  }

   void HAL_HRTIM_MspPostInit(HRTIM_HandleTypeDef* hhrtim)
   {
     GPIO_InitTypeDef GPIO_InitStruct = {0};
     if(hhrtim->Instance==HRTIM1)
     {
       __HAL_RCC_GPIOC_CLK_ENABLE();
       /**HRTIM GPIO Configuration
       PC6     ------> HRTIM_CHA1  */
       GPIO_InitStruct.Pin = GPIO_PIN_6;
       GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
       GPIO_InitStruct.Pull = GPIO_NOPULL;
       GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
       GPIO_InitStruct.Alternate = GPIO_AF1_HRTIM1;
       HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

     }

   }
hal_hrtim_handle_t *mx_hrtim1_init(void)
{
  /* HRTIM instance initialization */
  if (HAL_HRTIM_Init(&hHRTIM1, HAL_HRTIM1) != HAL_OK)
  {
    return NULL;
  }

  HAL_RCC_HRTIM1_EnableClock();

  /* HRTIM output channels configuration */
  hal_hrtim_output_config_t output_config;

  /* HAL_HRTIM_CHA1 configuration */
  output_config.set = HAL_HRTIM_OUT_SET_NONE;
  output_config.reset = HAL_HRTIM_OUT_RESET_NONE;
  output_config.polarity = HAL_HRTIM_OUTPUT_ACTIVE_HIGH;
  output_config.idle_level = HAL_HRTIM_OUTPUT_IDLE_INACTIVE;
  output_config.fault_level = HAL_HRTIM_OUTPUT_FAULT_ACTIVE;

  if (HAL_HRTIM_OUT_SetConfig(&hHRTIM1, HAL_HRTIM_CHA1, &output_config) != HAL_OK)
  {
    return NULL;
  }

  /* Timers configuration */
  hal_hrtim_timer_config_t timer_config;

  /* Timer A configuration */
  /* Time base configuration */
  timer_config.prescaler = HAL_HRTIM_TIM_PRESCALER_DIV1;
  timer_config.period = 65503;
  timer_config.repetition = 0;
  timer_config.operating_mode = HAL_HRTIM_TIM_MODE_CONTINUOUS;
  timer_config.counting_mode = HAL_HRTIM_TIM_COUNTING_UP;

  if (HAL_HRTIM_TIM_SetConfig(&hHRTIM1, HAL_HRTIM_TIMA, &timer_config) != HAL_OK)
  {
    return NULL;
  }

  HAL_RCC_GPIOC_EnableClock();

    hal_gpio_config_t  gpio_config;

  /**
    HRTIM1 GPIO Configuration

    [GPIO Pin] ------> [Signal Name]

       PC6     ------>   HRTIM1_CHA1
    **/
  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_4;
  HAL_GPIO_Init(HAL_GPIOC, HAL_GPIO_PIN_6, &gpio_config);

  /*  Enable Timer A interrupt */
  HAL_CORTEX_NVIC_SetPriority(HRTIM1_TIMA_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0);
  HAL_CORTEX_NVIC_EnableIRQ(HRTIM1_TIMA_IRQn);

  /* Initialize the DMA channel assigned to the timer A */
      if (HAL_DMA_Init(&hGPDMA1_CH0, HAL_GPDMA1_CH0) != HAL_OK)
  {
    return NULL;
  }

  HAL_RCC_GPDMA1_EnableClock();

hal_dma_direct_xfer_config_t xfer_cfg_hrtim1_tima_dma;
  xfer_cfg_hrtim1_tima_dma.request         = HAL_GPDMA1_REQUEST_HRTIM1_TIMA;
  xfer_cfg_hrtim1_tima_dma.direction       = HAL_DMA_DIRECTION_MEMORY_TO_MEMORY;
  xfer_cfg_hrtim1_tima_dma.src_inc         = HAL_DMA_SRC_ADDR_FIXED;
  xfer_cfg_hrtim1_tima_dma.dest_inc        = HAL_DMA_DEST_ADDR_FIXED;
  xfer_cfg_hrtim1_tima_dma.src_data_width  = HAL_DMA_SRC_DATA_WIDTH_BYTE;
  xfer_cfg_hrtim1_tima_dma.dest_data_width = HAL_DMA_DEST_DATA_WIDTH_BYTE;
  xfer_cfg_hrtim1_tima_dma.priority        = HAL_DMA_PRIORITY_LOW_WEIGHT_LOW;

  if (HAL_DMA_SetConfigPeriphDirectXfer(&hGPDMA1_CH0, &xfer_cfg_hrtim1_tima_dma) != HAL_OK)
  {
    return NULL;
  }

  /* Enable the interruption for GPDMA1_CH0 */
  HAL_CORTEX_NVIC_SetPriority(GPDMA1_CH0_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0);
  HAL_CORTEX_NVIC_EnableIRQ(GPDMA1_CH0_IRQn);

  /* Link the timer A DMA channel handle to the HRTIM1 handle */
  if (HAL_HRTIM_SetDMA(&hHRTIM1, HAL_HRTIM_DMA_TIMA, &hGPDMA1_CH0) != HAL_OK)
  {
    return NULL;
  }

  return &hHRTIM1;
}

Deinitialization sequence

void HAL_HRTIM_MspDeInit(HRTIM_HandleTypeDef* hhrtim)
{
  if(hhrtim->Instance==HRTIM1)
  {
    /* Peripheral clock disable */
    __HAL_RCC_HRTIM1_CLK_DISABLE();

    /* HRTIM1 DMA DeInit */
    HAL_DMA_DeInit(hhrtim->hdmaTimerA);

    /* HRTIM1 interrupt DeInit */
    HAL_NVIC_DisableIRQ(HRTIM1_TIMA_IRQn);
  }

}
void mx_hrtim1_deinit(void)
{
  /* Disable Timer A interrupt */
  HAL_CORTEX_NVIC_DisableIRQ(HRTIM1_TIMA_IRQn);

  (void)HAL_HRTIM_DeInit(&hHRTIM1);

  HAL_RCC_HRTIM1_Reset();

  HAL_RCC_HRTIM1_DisableClock();

  /* De-initialize all GPIO pins associated with HRTIM1 */
  HAL_GPIO_DeInit(HAL_GPIOC, HAL_GPIO_PIN_6);

#if defined(USE_HAL_GPIO_HSLV) && (USE_HAL_GPIO_HSLV == 1U)
  HAL_GPIO_DisableHighSpeedLowVoltage(HAL_GPIOC, HAL_GPIO_PIN_6);
#endif /* USE_HAL_GPIO_HSLV */
  /* De-initialize the DMA channel */
  HAL_DMA_DeInit(&hGPDMA1_CH0);

  /* Disable the interruption for DMA */
  HAL_CORTEX_NVIC_DisableIRQ(GPDMA1_CH0_IRQn);
}

IRQ handle function

 void HRTIM1_TIMA_IRQHandler(void)
 {
   HAL_HRTIM_IRQHandler(&hhrtim,HRTIM_TIMERINDEX_TIMER_A);
 }

void DMA1_Stream0_IRQHandler(void)
{
  HAL_DMA_IRQHandler(&hdma_hrtim1_a);
}
/* HRTIM1 TIMA interrupt */
void HRTIM1_TIMA_IRQHandler(void)
{
  HAL_HRTIM_TIMA_IRQHandler(&hHRTIM1);
}

/* GPDMA1 channel 0 global interrupt */
void GPDMA1_CH0_IRQHandler(void)
{
  HAL_DMA_IRQHandler(&hGPDMA1_CH0);
}